package com.jell.purchase.service.impl;

import com.github.pagehelper.PageHelper;
import com.jell.purchase.Constants;
import com.jell.purchase.model.GoodsInfoDo;
import com.jell.purchase.model.OrderInfoDo;
import com.jell.purchase.model.Query.GoodsInfoQuery;
import com.jell.purchase.model.po.GoodsCreatePO;
import com.jell.purchase.model.po.GoodsListPO;
import com.jell.purchase.model.vo.GoodsInfoVO;
import com.jell.purchase.model.vo.GoodsListVO;
import com.jell.purchase.persistence.GoodsInfoDao;
import com.jell.purchase.service.GoodsInfoService;
import com.jell.purchase.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by linx on 2018-04-17.
 */
@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {
    @Autowired
    private GoodsInfoDao goodsInfoDao;

    /**
     * 通过ID查询商品详情
     *
     * @param id
     * @return
     */
    @Override
    public GoodsInfoVO getGoodsInfoById(Integer id) {
        GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
        GoodsInfoDo goodsInfoDo = goodsInfoDao.selectByPrimaryKey(id);
        if (goodsInfoDo != null) {
            BeanUtils.copyProperties(goodsInfoDo, goodsInfoVO);
        }
        return goodsInfoVO;
    }

    @Override
    public PageVO<GoodsListVO> selectGoodsList(GoodsListPO po) {
        int pageNum = po.getPage() == null ? Constants.DEFAULT_PAGE : po.getPage();
        int rowNum = po.getRows() == null ? Constants.DEFAULT_ROW_NUM : po.getRows();
        PageVO<GoodsInfoDo> goodsInfoPage = getGoodsInfoDos(po, pageNum, rowNum);
        List<GoodsInfoDo> goodsInfoDos = goodsInfoPage.getRows();
        if (!CollectionUtils.isEmpty(goodsInfoDos)) {
            List<GoodsListVO> list = new ArrayList<>();
            for (GoodsInfoDo goodsInfoDo : goodsInfoDos) {
                GoodsListVO goodsListVO = new GoodsListVO();
                BeanUtils.copyProperties(goodsInfoDo,goodsListVO);
                list.add(goodsListVO);
            }
            PageVO<GoodsListVO> page = new PageVO<GoodsListVO>(goodsInfoPage.getPage(), goodsInfoPage.getPageSize(), goodsInfoPage.getTotal(),list);
            return page;
        }

        return null;
    }
    @Override
    public PageVO<GoodsListVO> selectGoodsListByName(GoodsListPO po) {
        int pageNum = po.getPage() == null ? Constants.DEFAULT_PAGE : po.getPage();
        int rowNum = po.getRows() == null ? Constants.DEFAULT_ROW_NUM : po.getRows();
        PageHelper.startPage(pageNum, rowNum, true, null, true);
        GoodsInfoQuery query = new GoodsInfoQuery();
        GoodsInfoQuery.Criteria criteria = query.createCriteria();
        if (StringUtils.isNotBlank(po.getChName())) {
            String name=po.getChName();
            criteria.andChNameLike("%"+po.getChName()+"%");
        }
        List<GoodsInfoDo> goodsInfoDos = goodsInfoDao.selectByQuery(query);
        PageVO<GoodsInfoDo> goodsInfoPage = getGoodsInfoDos(po, pageNum, rowNum);
        if (!CollectionUtils.isEmpty(goodsInfoDos)) {
            List<GoodsListVO> list = new ArrayList<>();
            for (GoodsInfoDo goodsInfoDo : goodsInfoDos) {
                GoodsListVO goodsListVO = new GoodsListVO();
                BeanUtils.copyProperties(goodsInfoDo,goodsListVO);
                list.add(goodsListVO);
            }
            PageVO<GoodsListVO> page = new PageVO<GoodsListVO>(goodsInfoPage.getPage(), goodsInfoPage.getPageSize(), goodsInfoPage.getTotal(),list);
            return page;
        }

        return null;
    }

    @Override
    public List<GoodsInfoDo> selectGoodsById(Integer goodsId) {
        ArrayList<GoodsInfoDo> list=new ArrayList<>();
        GoodsInfoDo goodsInfoDo=goodsInfoDao.selectByPrimaryKey(goodsId);
        list.add(goodsInfoDo);
        return list;


    }

    private PageVO<GoodsInfoDo> getGoodsInfoDos(GoodsListPO po, int pageNum, int rowNum) {
        PageHelper.startPage(pageNum, rowNum, true, null, true);
        GoodsInfoQuery query = new GoodsInfoQuery();
        GoodsInfoQuery.Criteria criteria = query.createCriteria();
        if (StringUtils.isNotBlank(po.getCode())) {
            criteria.andCodeEqualTo(po.getCode());
        }
        List<GoodsInfoDo> goodsInfoDos = goodsInfoDao.selectByQuery(query);
        return   new PageVO<GoodsInfoDo>(goodsInfoDos);
    }

    @Override
    @Transactional
    public Integer create(GoodsCreatePO po) {
        return null;
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {

    }
}
